package com.example.SpringBootMybatis.service;

import com.example.SpringBootMybatis.entity.Form;
import com.example.SpringBootMybatis.entity.Prediction;
import com.example.SpringBootMybatis.entity.PredictionResult;
import com.example.SpringBootMybatis.mapper.PredictionMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class PredictionService {
    @Resource
    private PredictionMapper predictionMapper;

    public PredictionResult onSubmit(Form form){
        List<Prediction> list= predictionMapper.onSubmit(form);
        Collections.sort(list, Comparator.comparing(Prediction::getRank));
        if (form.getTop()>list.size())form.setTop(list.size());
        int start = (form.getPageNum() - 1) * form.getPageSize();
        if (start >= list.size()) {
            return new PredictionResult(Collections.emptyList(), form.getTop());
        }
        int end = Math.min(start + form.getPageSize(), list.size());
        return new PredictionResult(list.subList(start, end), form.getTop());
    }
}
